package org.light.framework.bean;


import java.util.ArrayList;
import java.util.HashMap;

import org.light.framework.orm.BelongsInfo;
import org.light.framework.orm.ColumnInfo;
import org.light.framework.orm.DbMapper;

/**
 * 排序对象
 * 
 * @author de.cel
 *
 */
public class ResultOrder {
	HashMap<String, Boolean> dic = new HashMap<String, Boolean>();

	public void add(String field, Boolean isAscending) {
		dic.put(field, isAscending);
	}

	public void clear() {
		dic.clear();
	}

	/// <summary>
	/// 是否为空
	/// </summary>
	public boolean IsEmpty() {
		return dic.isEmpty();
	}

	/**
	 * 转对象类型对应的表或视图排序列
	 * 
	 * @param c
	 * @return
	 * @throws Exception
	 */
	public String toString(Class<?> c) throws Exception {
		StringBuilder builder = new StringBuilder();

		ArrayList<ColumnInfo> columns = DbMapper.getColumns(c);
		HashMap<String, BelongsInfo> belongsColumns = DbMapper.getBelongsDic(c);
		boolean hasBelongs = belongsColumns != null && !belongsColumns.isEmpty();
		String tableAlias = DbMapper.PRIMARY_TABLE_ALIAS;

		for (String propertyName : dic.keySet()) {
			if (propertyName.contains(".")) // 包含外键属性，表加上别名
			{
				String columnName = propertyName;

				if (hasBelongs) // 使用外键表的表排序
				{
					String[] temp = propertyName.split("."); // Aaa.Bbb.Ccc =>
																// Aaa.Bbb、Ccc
					String lastPName = temp[temp.length - 1]; // Ccc
					String firstPName = propertyName.substring(0, propertyName.lastIndexOf(".")); // Aaa.Bbb

					BelongsInfo bc = belongsColumns.get(firstPName);
					// 找到外键表的列
					int cascade = propertyName.split(".").length;
					columns = DbMapper.getColumns(bc.entityType, cascade);
					ColumnInfo column = ColumnInfo.findByPropertyName(columns, lastPName);
					if (column != null)
						columnName = column.name;
					else
						throw new Exception("不支持按 " + propertyName + " 排序，属性与数据库字段不匹配。");

					tableAlias = bc.alias;
				}
				builder.append(tableAlias + "." + columnName + " " + (dic.get(propertyName) ? "ASC" : "DESC"));
			} else {
				if (hasBelongs)
					builder.append(tableAlias + ".");

				ColumnInfo column = ColumnInfo.findByPropertyName(columns, propertyName);
				if (column != null)
					builder.append(column.name + " " + (dic.get(propertyName) ? "ASC" : "DESC"));
				else
					throw new Exception("不支持按 " + propertyName + " 排序，属性与数据库字段不匹配。");
			}
			builder.append(",");
		}
		if (!dic.isEmpty()) {
			builder.deleteCharAt(builder.length() - 1);
		} else {
			// 如果未指定排序值，则按主键ID进行排序
			ColumnInfo primaryKey = DbMapper.getPrimaryKey(c);
			if (hasBelongs) {
				builder.append(tableAlias + "." + primaryKey.name + " DESC");
			} else {
				builder.append(primaryKey.name + " DESC");
			}
		}
		return builder.toString();
	}

	/**
	 * 转表或视图的排序列
	 */
	public String toString() {
		StringBuilder builder = new StringBuilder();

		for (String columnName : dic.keySet()) {
			builder.append(columnName + " " + (dic.get(columnName) ? "ASC" : "DESC"));
			builder.append(",");
		}
		if (!dic.isEmpty()) {
			builder.delete(builder.length() - 1, 1);
		}
		return builder.toString();
	}
}